package org.fuyd.dao;

import java.util.List;

import org.fuyd.model.Teacher;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class TeacherDaoImpl implements TeacherDao {
  private HibernateTemplate hibernateTemplate;
  private SessionFactory sessionFactory;

  public void add(Teacher teacher) {
    hibernateTemplate.save(teacher);
  }

  public List<?> findByStudent(int studentid) {
    // List<?> list = hibernateTemplate.find("from Teacher as t inner join t.students as s where s.id = ?", studentid);

    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    Query query = session.createQuery("select t from Teacher t inner join t.students as s where s.id = " + studentid);
    // Query query = session.createQuery("select s from Student s inner join s.teachers as t where t.id = " + studentid);
    // Query query = session.createQuery("select t from Teacher t join t.students s where s.id = " + tudentid);

    return query.list();
  }

  public HibernateTemplate getHibernateTemplate() {
    return hibernateTemplate;
  }

  public SessionFactory getSessionFactory() {
    return sessionFactory;
  }


  public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
    this.hibernateTemplate = hibernateTemplate;
  }


  public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
  }

}
